package com.jpatest.demo.controller;

import com.jpatest.demo.Repository.DepartmentRepository;
import com.jpatest.demo.Repository.RoleRepository;
import com.jpatest.demo.Repository.UserRepository;
import com.jpatest.demo.Service.CompanyServiceImpl;
import com.jpatest.demo.Service.OrderServiceImpl;
import com.jpatest.demo.Service.UserServiceimpl;
import com.jpatest.demo.entity.*;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "test")
public class JpaTestController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserRepository userRepository;
    @Autowired
    DepartmentRepository departmentRepository;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    UserServiceimpl userServiceimpl;
//    @Autowired
//    OrderServiceImpl orderService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public@ResponseBody void add(){
        userRepository.deleteAll();
        roleRepository.deleteAll();
        departmentRepository.deleteAll();

        Department department = new Department();
        department.setName("开发部");
        departmentRepository.save(department);
        Assert.assertNotNull(department.getId());

        Role role = new Role();
        role.setName("admin");
        roleRepository.save(role);
        Assert.assertNotNull(role.getId());

        User user = new User();
        user.setName("林从武");
        user.setCreatedate(new Date());
        user.setDepartment(department);
        List<Role> roleList = roleRepository.findAll();
        Assert.assertNotNull(roleList);
        user.setRoles(roleList);
        userRepository.save(user);
        Assert.assertNotNull(user.getId());

        Pageable pageable = new PageRequest(0,10,new Sort(Sort.Direction.ASC,"id"));
        Page<User> page = userRepository.findAll(pageable);
        Assert.assertNotNull(page);
        for(User useritem: page.getContent()){
            log.info("====user=====user name:{},department name:{},role name:{}",useritem.getName(), useritem.getDepartment().getName(), useritem.getRoles().get(0).getName());
        }
    }

    @RequestMapping(value = "testSession", method = RequestMethod.POST)
    public @ResponseBody String testSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println(session.getClass());
        System.out.println(session.getId());
        String user = "lincongwu";
        session.setAttribute("user", user);
        return "success";
    }

//    @RequestMapping(value = "testZK", method = RequestMethod.POST)
//    public @ResponseBody String testZK(@RequestBody String serviceName){
//        orderService.testZKService(serviceName);
//        return "success";
//    }

    @RequestMapping(value = "testThreadPool", method = RequestMethod.POST)
    public @ResponseBody String testThreadPool(@RequestBody String serviceName){
        userServiceimpl.testThreadPool();
        return "success";
    }
}
